'use strict'

const _ = require('underscore')
const AV = require('leanengine')
const rp = require('request-promise')
const Promise = require('bluebird')

const WECHAT_CONFIG = require('./config')
const oauthCli = require('./oauth')
const menuJSON= require('./menu')

module.exports = function (data, api) {

  var sendText = function (arg) {
    arg = _.isFunction(arg) ? arg() : arg
    api.sendText(data.FromUserName, arg, function () {
      console.log('sendText', arguments)
    })
  }

  var o = [
    {
      test: /[a|b|c|d|1|2|3|4]/i,
      method () {
        // var content = data.Content
        // var reply
        // if (!isNaN(content)) {
        //   content = parseInt(content)
        //   if (content === 1) reply = 'a'
        //   if (content === 2) reply = 'b'
        //   if (content === 3) reply = 'c'
        //   if (content === 4) reply = 'd'
        // } else {
        //   reply = content.toLowerCase()
        // }
        // const uri = 'https://birdie-activity.wilddogio.com'
        // rp(uri + '/currentIndex.json')
        // .then(ret => {
        //   ret = JSON.parse(ret)
        //   this.currentIndex = ret
        //   if (!ret && ret !== 0) return Promise.reject()
        //   return rp(uri + `/activity/${ret}.json`)
        // }).then(ret => {
        //   ret = JSON.parse(ret)
        //   var result = _.map(ret.all, x => x).indexOf(data.FromUserName)
        //   if (ret.state === 'ongoing' && result === -1) {
        //     api.getUser(data.FromUserName, (err, ret) => {
        //       console.log(ret)
        //       ret.answer = reply
        //       return rp({
        //         method: 'PUT',
        //         uri: uri + `/activity/${this.currentIndex}/answers.json`,
        //         body: JSON.stringify({
        //           [ret.openid]: ret
        //         })
        //       }).then(() => {
        //         return rp({
        //           method: 'POST',
        //           uri: uri + `/activity/${this.currentIndex}/all.json`,
        //           body: data.FromUserName
        //         })
        //       }).then(() => {
        //         return api.sendText(data.FromUserName, `您已提交第${this.currentIndex+1}题`, new Function)
        //       })
        //     })
        //   } else {
        //     return api.sendText(data.FromUserName, 'error', new Function)
        //   }
        // })
      }
    }, {
      test: ['我的战绩', '历史'],
      method () {
        console.log(this.test)
        return api.sendText(data.FromUserName, data.Content, new Function)
      }
    }, {
      test: /[h|b]/,
      replyNews: [{
       "title": data.Content,
       "description":"Is Really A Happy Day",
       "url":"URL",
       "picurl":"PIC_URL"
     }, {
       "title":"Happy Day",
       "description":"Is Really A Happy Day",
       "url":"URL",
       "picurl":"PIC_URL"
     }]
   }, {
     test: ['l'],
     method () {
       var sql = `select * from _User where openid = '${data.FromUserName}'`
       AV.Query.doCloudQuery(sql).then(function (ret) {
         if (!ret.length) {
           sendText(`<a href="${WECHAT_CONFIG.host}/#!/bindPhone">点击此处</a>绑定你的手机号`)
         }
       }).catch(function (err) {
         console.log(err)
       })
     }
   }, {
     test: 'menu',
     method () {
       api.createMenu(menuJSON, function () {
         console.log(arguments)
       });
     }
   }, {
    test: 'o',
    replyText () {
      return oauthCli.getAuthorizeURL(`${WECHAT_CONFIG.host}/oauth?location=home`, 'STATE', 'snsapi_userinfo')
    }
  }, {
    test: 'wilddogempty',
    method () {
      console.log(1)
      return rp({
        uri: 'https://birdie-activity.wilddogio.com/.json',
        method: 'PUT',
        body: '{}'
      }).then(ret => {
        return sendText('清空成功')
      }).catch(err => console.log(err))
    }
  }
 ]

 o.forEach((el) => {
    if ((el.test instanceof RegExp && el.test.test(data.Content)) ||
    el.test === data.Content ||
    (_.isArray(el.test) && el.test.indexOf(data.Content) !== -1)){
      if (el.replyText) {
        let text
        if (_.isFunction(el.replyText)) text = el.replyText()
        else text = el.replyText
        return api.sendText(data.FromUserName, text, new Function)
      }
      if (el.replyNews) {
        let news
        if (_.isFunction(el.replyNews)) news = el.replyNews()
        else news = el.replyNews
        return api.sendNews(data.FromUserName, news, new Function)
      }
      return el.method()
    }
  })
  return
}
